<?php
/**
 * @name /www/stop-donating.php
 * @author Michael Foss <michael@mikefoss.com>
 * @version 2011.04.27
 *
 * Allows users to stop being donors.
 */

// Sets up configuration settings and includes libraries.
require_once('_ini.php');

ReqLib('/lib/private/User.php');

function DefinePreload() {
  $user = new User();
  $user->Load($_SESSION['userId']);
  if (!$user->UserId) {
    header('location: ' . _webroot('/login_register.php?pagereq=/stop-donating.php'));
    die();
  }
  elseif ($_POST['StopDonating']) {
    $user->RevokeRole('donator');
    $user->Save();
    header('location: ' . _webroot('/stop-donating.php?StopDonating=true'));
    die();
  }
}

function DefineHeader() {
?><style type="text/css">
</style>
<script type="text/javascript">
  $(function() {
    $('#StopDonating')
      .button({
        label: 'Stop Donating'
      })
      .click(function() {
        window.location = '<?=_webroot('/stop-donating.php?StopDonating=true') ?>';
      });
    $('#BecomeDonor')
      .button({
        label: 'Become Donor'
      })
      .click(function() {
        // Toggle the Become Donor button
        window.location = '<?=_webroot('/become-donor.php') ?>';
      });
  });
</script>
<title>Stop Donating</title>
<?
}

/**
 * Defines page content.
 *
 */
function DefineContent() {
  $user = new User();
  $user->Load($_SESSION['userId']);
  $isDonor = $user->HasRole('donator');
?><h1>Stop Donating</h1>
<?
  if (!$isDonor) {
    if ($_GET['StopDonating'] == 'true') {
?><p>
  You are no longer registered as a donor.
</p>
<p>
  We're sorry to see you go, but if you ever change your mind, you can re-register
  from the <a href="<?=_webroot('/account.php') ?>">Account</a> page.
</p>
<p>
  Next steps:
</p>
<ul>
  <li>
    <a href="account.php">Review Account Information</a>
  </li>
<?
      if (!$user->HasRole('traveler')) {
?>
  <li>
    <a href="#">Become a Traveler</a>
  </li>
<?
      }
      if (!$user->HasRole('volunteer')) {
?>
  <li>
    <a href="#">Become a Volunteer</a>
  </li>
<?
      }
?>
</ul>
<?
    }
    else {
?><p>
  It looks like you are not registered as a donor!
</p>
<p>
  If you would like to become a donor, you can do so by clicking the button below.
</p>
<p>
  <a id="BecomeDonor"></a>
</p>
<?
    }
  }
  else {
    if ($user->HasDonationHistory() && !$user->HasRole('administrator')) {
?><p>
  Unfortunately, you cannot stop being a donor due to the fact that you have
  data in our records. If you need further assistance, please contact technical
  support.
</p>
<?
    }
    else {
?><p>
  To stop donating, simply click the button below:
</p>
<center>
  <form method="POST">
    <input type="submit" name="StopDonating" value="StopDonating" id="StopDonating" />
  </form>
</center>
<?
    }
  }
}

// Launches the page; without this, nothing happens.
require_once('_loader.php');